const app = getApp();

Page({
  data: {
    tabbar: {},
    pageBg: '/images/userBg.jpg',
    user: null,
    currentTab: 0
  },

  onLoad: function() {
    wx.hideTabBar();
    app.editTabbar();
    this.getUserInfo();
  },

  bindGetUserInfo: function(e) {
    var that = this
    if (e.detail.userInfo) {
      that.setData({
        isGetInfo: true,
        userName: e.detail.userInfo.nickName,
        userUrl: e.detail.userInfo.avatarUrl
      })
      app.globalData.nickname = e.detail.userInfo.nickName
      wx.request({
        url: app.globalData.url + 'update_acc_info',
        data: {
          "openid": app.globalData.openId,
          "nickname": e.detail.userInfo.nickName,
          "avatar": e.detail.userInfo.avatarUrl
        },
        method: 'POST'
      });
    }

    this.onLoad()
  },

  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },

  //点击切换
  clickTab: function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.getUserInfo();
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },

  toDetail: function(e) {
    var user = {
      id: e.currentTarget.dataset.id,
      imgUrl: e.currentTarget.dataset.img,
      userImg: e.currentTarget.dataset.userimg,
      userName: e.currentTarget.dataset.username,
    }
    wx.navigateTo({
      url: '/pages/detail/detail?user=' + JSON.stringify(user),
    });
  },

  // 更新个人中心
  getUserInfo: function() {
    var that = this
    wx.request({
      url: app.globalData.url + 'get_acc_info',
      data:{
        'openid': app.globalData.openId
      },
      method:'POST',
      success: (result) => {
        console.log(result);
        
        that.setData({
          user: result.data.data,
          zuoping: result.data.data.item,
          shoucang: result.data.data.favor
        })
      }
    });
  }

})